import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Image4CS {

    String outputDir="C:\\Users\\Administrator\\Desktop\\cs\\";

    private BufferedImage image;
    private BufferedImage outputImage;
    private Graphics2D graphics;
    private int frameCount;

    public Image4CS(String imagePath) {
        try {
            this.image = ImageIO.read(new File(imagePath));
            this.outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
            this.graphics = outputImage.createGraphics();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void splitAndSaveGif(String outputGifPath, int depth) {
        frameCount = 0;
        saveFrame(outputImage);
        splitImage(image, depth);
        saveGif(outputGifPath);
    }

    private void saveFrame(BufferedImage bufferedImage) {
        graphics.drawImage(image, 0, 0, null);
        try {
            ImageIO.write(bufferedImage, "png", new File("frame" + frameCount + ".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        frameCount++;
    }

    private void splitImage(BufferedImage image, int depth) {
        if (depth <= 0) {
            return;
        }

        // 获取图像的宽度和高度
        int width = image.getWidth();
        int height = image.getHeight();

        // 分割图像成四个子图像
        BufferedImage topLeft = image.getSubimage(0, 0, width / 2, height / 2);
        BufferedImage topRight = image.getSubimage(width / 2, 0, width / 2, height / 2);
        BufferedImage bottomLeft = image.getSubimage(0, height / 2, width / 2, height / 2);
        BufferedImage bottomRight = image.getSubimage(width / 2, height / 2, width / 2, height / 2);

        // 分割后保存每个子图像的帧
        saveFrame(topLeft);
        saveFrame(topRight);
        saveFrame(bottomLeft);
        saveFrame(bottomRight);
        System.out.println("saveFrame...");

        // 递归分割子图像
        splitImage(topLeft, depth - 1);
        splitImage(topRight, depth - 1);
        splitImage(bottomLeft, depth - 1);
        splitImage(bottomRight, depth - 1);
    }

    private void saveGif(String outputGifPath) {
        try {
            File[] frames = new File[frameCount];
            for (int i = 0; i < frameCount; i++) {
                frames[i] = new File("frame" + i + ".png");
            }

            GifSequenceWriter writer = new GifSequenceWriter(outputGifPath, 100, true);
            for (File frame : frames) {
                BufferedImage frameImage = ImageIO.read(frame);
                writer.writeToSequence(frameImage);
                frame.delete();
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Image4CS quadTreeImage = new Image4CS("C:\\Users\\Administrator\\Desktop\\cs\\咆哮2.jpg");
        quadTreeImage.splitAndSaveGif("C:\\Users\\Administrator\\Desktop\\cs\\output.gif", 4); // 4表示分割的深度
    }
}
